//
//  

#import "AM_TabBarView.h"
#import "UIButton+Spacing.h"


@interface AM_TabBarView ()

@property (nonatomic, strong) UIButton *publishButton;
@property (nonatomic, strong) NSMutableArray * buttonArray;

@end

@implementation AM_TabBarView{
    SendBarIndexBlock _block;
}

//创建tabbar视图
- (id)initWithFrame:(CGRect)frame andTabBarButtonParrkDictory:(NSDictionary *)dic andBlock:(SendBarIndexBlock)block{
    self = [super initWithFrame:frame];
    if (self) {
        _block = block;
        [self createTabBarqffsViewWithDictory:dic];
        self.backgroundColor = rgba(255, 255, 255, 1);
        
    }
    return self;
}

//创建tabbar按钮
- (void)createTabBarqffsViewWithDictory:(NSDictionary *)dic{
    if (dic.count == 0) {
        return;
    }
    //创建一个分割线
    UIView * divisionLine = [[UIView alloc]initWithFrame:CGRectMake(0,0, kMainScreenWidth, 0.5)];
    divisionLine.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:divisionLine];
    //创建按钮
    NSArray * imageNameNormal = [dic objectForKey:@"imageNameNormal"];
    NSArray * imageNameSelected = [dic objectForKey:@"imageNameSelected"];
    NSArray * buttonTitle = [dic objectForKey:@"buttonTitle"];
    CGFloat width = kMainScreenWidth/imageNameNormal.count;
    CGFloat imageHeight = 35;
    for (int i = 0 ; i< imageNameNormal.count ; i++) {
        //按钮
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(width*i, 7, width, imageHeight);
        [btn setImage:[UIImage imageNamed:imageNameNormal[i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:imageNameSelected[i]] forState:UIControlStateSelected];
        [btn setImage:[UIImage imageNamed:imageNameSelected[i]] forState:UIControlStateHighlighted];
        [btn setTitle:buttonTitle[i] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:12];
        [btn am_layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:8];
        [btn setTitleColor:rgba(142, 143, 142, 1) forState:UIControlStateNormal];
        [btn setTitleColor:rgba(245, 64, 106, 1) forState:UIControlStateSelected];
        btn.tag = 100 + i;
        if (i==0) {
            btn.selected = YES;
        }
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        [self.buttonArray addObject:btn];
      
    }
}


- (NSMutableArray *)buttonArray {
    if (_buttonArray == nil) {
        _buttonArray = [NSMutableArray new];
    }
    return _buttonArray;
}



//MARK:切换tabbar
- (void)btnClick:(UIButton *)btn{
    
    _block(btn.tag - 100);
    for (UIButton * b in self.subviews) {
        if ([b isKindOfClass:[UIButton class]]) {
            if (b.tag == btn.tag) {
                b.selected = YES;
            }else{
                b.selected = NO;
            }
        }
    }
    
}




@end
